package com.xxx.meter.ui.info.param;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.model.http.Api;
import com.xxx.meter.model.http.ApiCallback;
import com.xxx.meter.model.http.bean.MachineInfoBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.KeyBoardUtil;
import com.xxx.meter.ui.info.adapter.InfoParamAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class InfoMachineDetailActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemLongClickListener {

    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.not_data)
    ImageView mNotData;
    @BindView(R.id.not_net)
    ImageView mNotNet;
    @BindView(R.id.main_title)
    TextView mTitle;

    private String recordId;
    private String deviceId;
    private String deviceType;
    private String back;
    private InfoParamAdapter mAdapter;
    private ArrayList<MachineInfoBean> mList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_info_machine;
    }

    @Override
    protected void initData() {
        recordId = getIntent().getStringExtra("recordId");
        deviceId = getIntent().getStringExtra("deviceId");
        deviceType = getIntent().getStringExtra("deviceType");
        back = getIntent().getStringExtra("back");
        String time = getIntent().getStringExtra("time");
        mTitle.setText(time);

        mAdapter = new InfoParamAdapter(mList);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(this);
        mAdapter.setOnItemLongClickListener(this);
        loadInfo(true);
    }

    @OnClick({R.id.main_return, R.id.not_data, R.id.not_net})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.main_return:
                finish();
                break;
            case R.id.not_data:
                loadInfo(true);
                break;
            case R.id.not_net:
                loadInfo(true);
                break;
        }
    }

    @Override
    public void onRefresh() {
        loadInfo(false);
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        MachineInfoBean machineInfoBean = mList.get(position);
        switch (machineInfoBean.getItemType()) {
            case MachineInfoBean.TEXT:
                KeyBoardUtil.copy(this, machineInfoBean.getData() + machineInfoBean.getCompany());
                break;
            case MachineInfoBean.SELECT:

                break;
            case MachineInfoBean.BACK:
                KeyBoardUtil.copy(this, machineInfoBean.getName());
                break;
        }
        return false;
    }


    /**
     * @param isRefresh 是否刷新
     * @Model 获取机器巡查数据
     */
    private void loadInfo(boolean isRefresh) {
        Api.getInstance().getInfoDeviceInfoList(recordId, deviceType, deviceId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiCallback<List<MachineInfoBean>>(this, isRefresh) {
                    @Override
                    public void onSuccess(List<MachineInfoBean> list) {
                        if (list == null || list.size() == 0) {
                            mRefresh.setVisibility(View.GONE);
                            mNotNet.setVisibility(View.GONE);
                            mNotData.setVisibility(View.VISIBLE);
                            mRefresh.setRefreshing(false);
                            return;
                        }

                        //加入备注
                        MachineInfoBean machineInfoBean = new MachineInfoBean();
                        machineInfoBean.setBack(back);
                        list.add(machineInfoBean);

                        mRefresh.setVisibility(View.VISIBLE);
                        mNotData.setVisibility(View.GONE);
                        mNotNet.setVisibility(View.GONE);
                        mList.clear();
                        mList.addAll(list);
                        mAdapter.notifyDataSetChanged();
                        mRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        mRefresh.setVisibility(View.GONE);
                        mNotData.setVisibility(View.GONE);
                        mNotNet.setVisibility(View.VISIBLE);
                        mRefresh.setRefreshing(false);
                    }
                });
    }

}
